/**
* Created by Administrator on 2017/1/12.
*/
var shares=null,Intent=null,File=null,Uri=null,main=null;
//H5 plus事件处理
function plusReady(){
    updateServices();
    if(plus.os.name=='Android'){
        updateServices();//更新分享服务
        main=plus.android.runtimeMainActivity();//Android的主Activity对象是启动应用时自动创建的
        Intent=plus.android.importClass('android.content.Intent');//Android平台导入Intent类
        File=plus.android.importClass('java.io.File');
        Uri=plus.android.importClass('android.net.Uri');
    }
}
if(window.plus){
    plusReady();
}else{
    document.addEventListener('plusready',plusReady,false);
}
//更新分享服务
function updateServices() {
    plus.share.getServices(function(s){
         shares={};
         for(var i in s){
             var t=s[i];
             shares[t.id]=t;
         }
 },function (e) {
        console.log('获取分享服务列表失败:'+e.message);
    })
}
//分享的操作
function shareAction(sb,bh) {
     if(!sb || !sb.s){
         console.log("无效的分享服务");
         return;
     }
     var sharehref='http://www.cxzly.com/share/literary/news/id/'+sharehrefID;
     //链接地址
     var msg={content:sharehrefDes,extra:{scene:sb.x}};
     if(bh){
         msg.href=sharehref;
         if(sharehrefTitle!=''){
             msg.title=sharehrefTitle;
         };
         if(sharehrefDes!=''){
             if(sharehrefDes.length>43){
                sharehrefDes=sharehrefDes.replace(/[\r\n]/g,"").replace(/[ ]/g,"").substring(0,43)+".....";
             }else{
                sharehrefDes=sharehrefDes.replace(/[\r\n]/g,"").replace(/[ ]/g,"");
             }
             console.log(sharehrefDes);
             msg.content=sharehrefDes;
         };
         //图片显示
         msg.thumbs=['_www/logo.png'];
         msg.pictures=['_www/logo.png'];
     }else{
         //暂无
     }
     //发送分享
     if(sb.s.authenticated){
         console.log('------已授权-------');
         shareMessage(msg,sb.s);
     }else{
         console.log('--------未授权---------');
         sb.s.authorize(function () {
             shareMessage(msg,sb.s);
         },function(e){
             console.log('认证授权失败：'+e.code+'---'+e.message);
         })
     }
}
//发送分享消息
function shareMessage(msg,s){
     console.log(JSON.stringify(msg)+'发送分享消息');
     s.send(msg,function(){
         console.log('分享到\''+s.description+'\'成功！');
     },function(e){
         console.log('分享到\''+s.description+'\'失败！'+JSON.stringify(e));
    //      plus.nativeUI.alert("分享到\""+s.description+"\"失败！"+JSON.stringify(e));
     });
}
//分享链接
function shareHref(){
     var shareBts=[];
     //更新分享列表
     var ss=shares['weixin'];
     ss&&ss.nativeClient&&(
         shareBts.push({title:'微信朋友圈',s:ss,x:'WXSceneTimeline'}),
         shareBts.push({title:'微信好友',s:ss,x:'WXSceneSession'})
     );
     //弹出分享列表
     shareBts.length>0?plus.nativeUI.actionSheet({title:'分享链接',cancel:'取消',buttons:shareBts},
         function(e){
             (e.index>0)&& shareAction(shareBts[e.index-1],true);
         }):plus.nativeUI.alert('当前环境无法实现分享功能！')
}
//点击分享操作
mui('.header').on('tap','#share',function(){
     shareHref();
     console.log(sharehrefDes.replace(/[\r\n]/g,"").replace(/[ ]/g,"")+'-----share.js页面的新闻内容----');
});